const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});

//业务逻辑
module.exports = function() {
    var router = express.Router();

    //点击确定修改
    router.post('/updateforumcomment',function (req,res,next) {
        console.log(req.body);
      if(req.files.length==0)
      {
        db.query('UPDATE `forumcomment` SET `FCContent`="'+req.body.editor+'" WHERE `FCId`=' + parseInt(req.body.FCId), function (err, data) {
          if (err) {
            res.status(400).send('<script>alert("修改执行失败，请稍后再次执行!");window.location.href="404.html";</script>').end();
          }
          else {
            if (data.length == 0) {
              res.status(400).send('<script>alert("修改失败!");window.location.href="404.html";</script>').end();
            }
            else {
              res.status(200).send('<script>alert("修改成功!");window.location.href="forumcomment.html";</script>')
            }
          }
        });
      }
      else {
        var ext = pathLib.parse(req.files[0].originalname).ext;
        //console.log("dxcvbnm" + ext);
        //文件旧存放路径
        var oldPath = req.files[0].path;
        // console.log("文件旧存放路径"+oldPath)
        //文件新存放路径
        var newPath = req.files[0].path + ext;
        // console.log("文件新存放路径" + newPath)
        //文件新名
        var newFileName = req.files[0].filename + ext;

        console.log("我上的图片" + newFileName);
        fs.rename(oldPath, newPath, function (err) {
          if (err) {
            res.status(500).send('upload is error');
          }

          db.query('UPDATE `forumcomment` SET FCcommentPic="'+newFileName+'" `FCContent`="'+req.body.editor+'" WHERE `FCId`=' + parseInt(req.body.FCId), function (err, data) {
            if (err) {
              res.status(400).send('<script>alert("修改执行失败，请稍后再次执行!");window.location.href="404.html";</script>').end();
            }
            else {
              if (data.length == 0) {
                res.status(400).send('<script>alert("修改失败!");window.location.href="404.html";</script>').end();
              }
              else {
                res.status(200).send('<script>alert("修改成功!");window.location.href="forumcomment.html";</script>')
              }
            }
          });

        });
      }

    });
//开始渲染后台首页

  router.use('/updateforumcomment.html',function (req,res,next) {

    db.query('SELECT * FROM forumcomment, forumbinfo,userinfo where userinfo.userId=forumcomment.userId and forumbinfo.forumbId=forumcomment.forumbId and FCId='+parseInt(req.query.FCId),function (err,data) {
      if(err){
        res.send('<script>alert("程序出错");window.location.href="400.html";</script>').end();
      }
      else{
        if(data.length==0){
          res.send('<script>alert("暂无数据");window.location.href="400.html";</script>').end();
        }
        else{
          res.render("forum/updateforumcomment.ejs",{msg:data});
        }
      }
    });
  });
    return router;
}
